package org.theqcp.wow;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.helpers.NullEnumeration;
import org.theqcp.wow.model.exceptions.PlayerSetupException;
import org.theqcp.wow.model.mechanics.ClassType;
import org.theqcp.wow.sim.engine.old.Configurator;
import org.theqcp.wow.sim.engine.old.EnhSimConfig;

/**
 * Sets up simulation from commandline.
 * 
 * @author bhamlin
 */
public class Main {

    private static final String logPattern = "%-5p[%t|%c]:%m%n";

    static {
	if( LogManager.getRootLogger().getAllAppenders().getClass().equals(NullEnumeration.class) ) {
	    PatternLayout pattern = new PatternLayout(logPattern);
	    ConsoleAppender console = new ConsoleAppender(pattern);
	    Logger rootLogger = LogManager.getRootLogger();
	    rootLogger.addAppender(console);
	} else {
	    LogManager.getRootLogger().debug("Using already configured logger.");
	}
    }

    /**
     * Loads a simulation thread group for each simulation parameter supplied.
     * Based on old exec model; will be revised when I have time.
     * 
     * @param args - simulation parameters to execute
     * @throws IOException thrown on error reading config file; to be handled
     *         more gracefully in the future
     */
    public static void main( String[] args ) throws IOException {
	HashMap<String, String> config = new HashMap<String, String>();
	LogManager.getRootLogger().setLevel(Level.DEBUG);

	Logger log = LogManager.getLogger("Init");

	if( args.length < 1 ) {
	    log.fatal("No config file specified. Pass the config file as parameter to this program.");
	    return;
	}

	// TODO: move config loading to
	// org.theqcp.wow.sim.engine.revised.SimulationParameters
	BufferedReader reader = new BufferedReader(new FileReader(args[0]));
	StringTokenizer tokenizer;
	String key, value;
	String line;

	while( null != (line = reader.readLine()) ) {
	    tokenizer = new StringTokenizer(line);
	    if( tokenizer.countTokens() >= 2 ) {
		key = tokenizer.nextToken().toLowerCase();
		value = tokenizer.nextToken().toLowerCase();

		if( tokenizer.countTokens() > 0 ) {
		    value += tokenizer.nextToken();
		}

		config.put(key, value);

		log.debug(String.format("[%s] {%s}", key, value));
	    }
	}

	// Shaman player = (Shaman)
	// PlayerFactory.createPlayer(ClassType.SHAMAN);

	// EnhSimConfig sc = new EnhSimConfig(player);

	// for( String arg: args ) {
	// if( arg.equalsIgnoreCase("ep") ) {
	// sc.setEPConfig(new EquipmentPointConfig());
	// break;
	// }
	// }

	// sc.setConfig(config);

	try {
	    EnhSimConfig enhConfig = Configurator.createNewConfig(ClassType.SHAMAN, config);

	    for( String arg: args ) {
		if( arg.equalsIgnoreCase("ep") ) {
		    // do ep calc instead
		    break;
		}
	    }

	    log.debug(enhConfig);
	} catch( PlayerSetupException e ) {
	    log.error(e);
	    log.debug(e, e);
	}

    }

}
